package com.chis.netty.simplehttp;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author jiangwei
 * @date 2016-3-2
 * @Description:http协议启动 (windows系统)
 * @Company:zwx
 */
public class HttpServerWindows {

	/**
	 * @param port 端口
	 * @throws Exception 系统异常
	 */
	private void bind(int port) throws Exception {
		// 配置服务端的NIO线程组
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap b = new ServerBootstrap();
			b.group(bossGroup, workGroup).channel(NioServerSocketChannel.class);
			b.childHandler(new HttpServerInitializer());
			System.out.println("服务端开启等待客户端连接 ... ...");
			// 绑定端口，同步等待成功
			ChannelFuture f = b.bind(port).sync();
			// 等待服务端监听端口关闭
			f.channel().closeFuture().sync();
		} finally {
			// 风骚退出，释放NIO线程组
			bossGroup.shutdownGracefully();
			workGroup.shutdownGracefully();
		}
	}

	/**
	 * win系统入口
	 * @param args args
	 * @throws Exception 系统异常
	 */
	public static void main(String[] args) throws Exception {
		new HttpServerWindows().bind(Integer.valueOf(args[0]));
	}
}
